package com.qmth.gkwb.web.view;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qmth.gkwb.data.document.Order;
import com.qmth.gkwb.data.document.Student;
import com.qmth.gkwb.data.exception.CommonBusinessException;
import com.qmth.gkwb.data.exception.OrderCreateFailException;
import com.qmth.gkwb.data.pay.OrderService;
import com.qmth.gkwb.data.repository.StudentRepo;
import com.qmth.pay.OrderFormData;
import com.qmth.pay.OrderStatus;
import com.qmth.pay.Sign;
import com.qmth.pay.chinapay.ChinaPayDate20070129;
import com.qmth.pay.chinapay.ChinaPaySign;
import com.qmth.pay.chinapay.ChinaPayVersion;
import com.qmth.pay.chinapay.ChinaPayOrderFormData;
import com.qmth.pay.exception.KeyErrorException;

@Controller
@RequestMapping("view/pay")
public class PayController {

	@Autowired private OrderService orderService;
	@Autowired private StudentRepo studentRepo;
	
	@RequestMapping(value="test",method=RequestMethod.GET)
	public ModelAndView test(){
		return new ModelAndView("test");
	}
	
	@RequestMapping(value="order/{studentId}",method=RequestMethod.GET)
	public ModelAndView order(@PathVariable String studentId){
		ModelAndView view = new ModelAndView("pay");
		
		try {
			Student student = studentRepo.findOne(studentId);
			view.addObject("student",student);
			int count = orderService.countPayingOrders(student.getExamId());
			if(count > 5)
			view.addObject("notice","提醒：请检查银行卡，确认是否已经扣款再进行缴费，以免重复缴费");
			OrderFormData orderData = orderService.createChinaPayOrder(student.getExamId());
			view.addObject("orderData",orderData);
			
			Order order = new Order();
			order.setOrderId(orderData.getOrderNO());
			order.setExamId(student.getExamId());
			order.setMoney(orderData.getAmout());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			order.setTransDate(sdf.format(orderData.getOrderDate()));
			order.setMerId(orderData.getBusinessID());
			order.setChkValue(orderData.getEncrypt());
			orderService.createOrder(order);
			
		} catch (OrderCreateFailException e) {
			view.addObject("error",e.getMessage());
			e.printStackTrace();
		} catch (KeyErrorException e) {
			view.addObject("error",e.getMessage());
			e.printStackTrace();
		}catch(Exception e){
			view.addObject("error","未找到报名考生信息");
			e.printStackTrace();
		}
		
		return view;
	}
	
	@RequestMapping(method=RequestMethod.GET)
	@ResponseBody
	public OrderFormData getOrderByExamId(@RequestParam String examId) throws OrderCreateFailException, KeyErrorException{
		return orderService.createChinaPayOrder(examId);
	}
	
	@RequestMapping(value="paid",method=RequestMethod.GET)
	@ResponseBody
	public boolean isPayComplete(@RequestParam String examId){
		return orderService.isOrderPaid(examId);
	}
	
	@RequestMapping(value="callback",method=RequestMethod.POST)
	@ResponseBody
	public boolean callback(HttpServletRequest req) throws KeyErrorException, CommonBusinessException{
		ChinaPayDate20070129 data = new ChinaPayDate20070129();
		data.setPayRemark(req.getParameter("Priv1"));
		data.setTransStatus(req.getParameter("status"));
		data.setOrderNO(req.getParameter("orderno"));
		data.setBusinessID(req.getParameter("merid"));
		data.setResAmount(req.getParameter("amount"));
		data.setTransDate(req.getParameter("transdate"));
		data.setEncrypt(req.getParameter("checkvalue"));
		return orderService.orderPayReturn(data);
		
	}
	
	@RequestMapping(value="result",method=RequestMethod.POST)
	public ModelAndView result(HttpServletRequest req){
		String status = req.getParameter("status");
		boolean result  = false;
		if(status.equals("1001")){
			result = true;
		}
		return new ModelAndView("result", "result", result);
	}
}
